package shuashuami.hb.com.fragment.bussiness;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.avtivity.BDistributionCenterActivity;
import shuashuami.hb.com.avtivity.BHaveHandActivity;
import shuashuami.hb.com.avtivity.BSettingActivity;
import shuashuami.hb.com.avtivity.BSubordinateActivity;
import shuashuami.hb.com.avtivity.BUserInfoActivity;
import shuashuami.hb.com.avtivity.BWeiWanchengTaskActivity;
import shuashuami.hb.com.avtivity.BWodeFabuActivity;
import shuashuami.hb.com.avtivity.BWodeyuEActivity;
import shuashuami.hb.com.avtivity.BYiWanchengTaskActivity;
import shuashuami.hb.com.fragment.AbFragment;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/13<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/13<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class HbBUserFragm extends AbFragment {


    private ImageView imgUserHead;
    private TextView tvUserName, tvId, tvwaitConfrimNum, tvcarryOnNum, tvcompleteNum;
    private TextView tvYongjin;

    private GlidUtil glidUtil;
    private RelativeLayout llToUserInfo;
    private LinearLayout llToBDistributionCenter;

    private LinearLayout llDaiShenhe, llJingxingzhong, llYiWancheng, llToShuashou, llToWodeyongjin, llToWodeFabu;

    @Override
    public void initView(View v) {
        setTitleBar("我的", false);
        imgUserHead = (ImageView) v.findViewById(R.id.user_head);
        tvUserName = (TextView) v.findViewById(R.id.user_head_title);
        tvId = (TextView) v.findViewById(R.id.user_id);
        tvwaitConfrimNum = (TextView) v.findViewById(R.id.tv_buser_daishenhe);
        tvcarryOnNum = (TextView) v.findViewById(R.id.tv_buser_jinxingzhong);
        tvcompleteNum = (TextView) v.findViewById(R.id.tv_buser_yiwancheng);
        tvYongjin = (TextView) v.findViewById(R.id.tv_buser_yongjin);

        llToUserInfo = (RelativeLayout) v.findViewById(R.id.a_personInfo);
        llToBDistributionCenter = (LinearLayout) v.findViewById(R.id.ll_buser_todistribution_center);

        llDaiShenhe = (LinearLayout) v.findViewById(R.id.ll_buser_daishenhe);
        llJingxingzhong = (LinearLayout) v.findViewById(R.id.ll_buser_to_jingxingzhong);
        llYiWancheng = (LinearLayout) v.findViewById(R.id.ll_buser_yiwancheng);
        llToShuashou = (LinearLayout) v.findViewById(R.id.ll_buser_to_shuashou);
        llToWodeyongjin = (LinearLayout) v.findViewById(R.id.buser_to_wodeyongjin);
        llToWodeFabu = (LinearLayout) v.findViewById(R.id.ll_buser_to_wodefabu);
    }

    @Override
    public void initData() {
        glidUtil = new GlidUtil(getActivity());
        setRightView();
    }

    @Override
    public void initListener() {
        setRightViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BSettingActivity.class);
                startActivity(intent);
            }
        });
        llToUserInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BUserInfoActivity.class);
                startActivity(intent);
            }
        });
        llToBDistributionCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BDistributionCenterActivity.class);
                startActivity(intent);
            }
        });
        llDaiShenhe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BWeiWanchengTaskActivity.class);//待审核
                startActivity(intent);
            }
        });
        llJingxingzhong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BHaveHandActivity.class);//进行中
                startActivity(intent);
            }
        });
        llYiWancheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BYiWanchengTaskActivity.class);//已完成
                startActivity(intent);
            }
        });
        llToShuashou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BSubordinateActivity.class);
                startActivity(intent);
            }
        });
        llToWodeyongjin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BWodeyuEActivity.class);
                startActivity(intent);
            }
        });
        llToWodeFabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BWodeFabuActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        getMessage();
    }

    @Override
    public int bindView() {
        return R.layout.frag_buser;
    }

    private void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(getActivity());
        HttpBMethods.getBuserInfo(listener, shareCUserInfo.getId());
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(getActivity());
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject temp = jsonObject.getJSONObject("content");
                            glidUtil.uploadCircle(temp.getString("avatar"), imgUserHead, R.mipmap.ic_launcher);
                            tvUserName.setText(temp.getString("username"));
                            tvId.setText("ID:" + temp.getString("id"));
                            isShow(tvwaitConfrimNum, temp.getString("waitConfrimNum"));
                            isShow(tvcarryOnNum, temp.getString("carryOnNum"));
                            isShow(tvcompleteNum, temp.getString("completeNum"));
                            tvYongjin.setText(temp.getString("money"));
                        } else {
                            ToastUtil.showShort(getActivity(), jsonObject.getString("message"));
                        }

                    } catch (JSONException e) {
                        ToastUtil.showShort(getActivity(), "数据解析错误");
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void isShow(TextView tv, String str) {
        if (str.equals("0")) {
            tv.setVisibility(View.GONE);
        } else {
            tv.setText(str);
        }
    }
}
